import { ref, computed, onMounted } from "vue";
import { defineStore } from "pinia";
import { getLocal } from "@/apis/place";
import { getWeather, getNowWeather } from "@/apis/weather";
// 定义counter空间
export const useLocalWeatherStore = defineStore("localWeather", () => {
  // 定义city变量
  const LocalCity = ref("");
  const LocalWeather = ref("");
  const nowWeather = ref("");
  // 定义cityList变量

  // 获取本地存储的城市列表
  // 定义获取本地城市的异步函数
  const getLocalCity = async () => {
    // 调用 getLocal 函数获取本地存储的城市信息
    const res = await getLocal();
    // 将获取到的城市信息赋值给 LocalCity
    LocalCity.value = res.data;
    //console.log(LocalCity.value)
  };
  // 定义获取当前天气数据的异步函数
  const getNowWeatherData = async (city) => {
    //console.log(city)
    // 调用 getNowWeather 函数获取当前天气数据
    const res = await getNowWeather(city);
    // 返回当前天气数据
    return res.data;
  };
  // 定义获取未来天气数据的异步函数
  const getWeatherData = async (city) => {
    // 调用 getWeather 函数获取未来天气数据
    const res = await getWeather(city);
    // 返回未来天气数据
    return res.data;
  };

  onMounted(() => {
    getLocalCity().then(() => {
      getNowWeatherData(LocalCity.value.adcode).then((res) => {
        nowWeather.value = res.lives[0];
        //console.log(nowWeather.value)
      });
      getWeatherData(LocalCity.value.adcode).then((res) => {
        LocalWeather.value = res.forecasts[0].casts;
        console.log(LocalWeather.value);
      });
    });
  });
  return { LocalCity, nowWeather, LocalWeather };
});
